package flib;

import java.util.Vector;


import simpleGeom.Vec2D;

public class EdgeSegment {
	FlibHEdge hEdge;

	public float getLength() {
		float sumL = 0;
		FlibHEdge hE = hEdge;
		do {
			sumL += hE.getLength();
			hE = hE.getNext();
		} while (!((NodeData) hE.getOrg().getData()).corner);
		return sumL;
	}

	public float getLength(FlibHENode n){
		float sumL = 0;
		FlibHEdge hE = hEdge;
		while ( hE.getOrg()!=n) {
			sumL += hE.getLength();
			hE = hE.getNext();
		} ;
		return sumL;
	}
	public Vector<FlibHENode> getNodes(){
		Vector<FlibHENode>nodes=new Vector<FlibHENode>();
		FlibHEdge hE = hEdge;
		do {
			nodes.add(hE.getOrg());
			hE = hE.getNext();
		} while (!((NodeData) hE.getOrg().getData()).corner);
		return nodes;
	}
	
	public FlibHENode getNode(float l){
		FlibHEdge hE = hEdge;
		float sumL=0;
		float absL=l*getLength();
		do {
			float cL = hE.getLength();
			if (absL<=sumL+cL){
				float fac=(absL-sumL)/cL;
				if (fac==0)return hE.getOrg();
				if (fac==1)return hE.getDest();
				else{
					//System.out.println("fac");
					Vec2D v=hE.getDir();
					v.mult(fac);
					FlibHENode n=new FlibHENode(hE.getX1()+v.x,hE.getY1()+v.y);
					
					FlibHEdge cHE=hE.split(n);
					return n;
				}
			}
			hE = hE.getNext();
		} while (hE!=hEdge);
		return null;
		
	}
}
